#ifndef CORE_H
#define CORE_H

#include <QObject>
#include <gst/gst.h>
#include <string.h>
#include <math.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <pulse/pulseaudio.h>

#include "global/global.h"
#include "utils/confighandler.h"

class Core : public QObject
{
    Q_OBJECT
public:
    explicit Core(QObject *parent = nullptr);
    ~Core();
    typedef enum Format {MP4, MKV, AVI, MOV, WEBM, GIF} FORMAT;
    static Core *getInstance();

    void setPropertyOfSelectWid( uint qId, bool isfullscreen, bool isWindow, QString screen, uint id, QRect rect, double scale);  //框选信息传递
    void setSavePath(QString savePath);
    void setSaveFormat(QString saveformat);
    void setResolution(int resolution);
    void setFrameRate(QString frameRate);
    void setMicrophone(QString microphone);
    void getMicDevices(QStringList &ls, QString soundtype, bool isName = false);
    QString getUsingMicDevices(bool isName = false);

    void startRecord(uint id, const QRect &rect);
    void stopRecord(QString &completePath);
    void pauseRecord();
    void contineRecord();
    void openMicrophone();
    void closeMicrophone();
    void toggle_microphone(bool mute);      //切换麦克风硬件的状态
    void levelMeterStart(QString name);                 //检测麦克风音量
    static gboolean message_handler( GstBus * bus, GstMessage * message, gpointer data );
    void ModifylevelMeterDevice (QString newDevice);//更改检测的麦克风设置
signals:
    void updateVolume(int newVolume);

private:
    QString getXimagesrc();
    QString getCapsFilter();
    QString get_Videocodec_Encoder();
    QString getScale();
    QStringList getSelectedAudioDevice();
    QStringList getSelectedAudioDeviceName();
    QString getMuxer();
    QString pipeline_structured_output( QString pipeline );
    void gst_Elements_available();
    QString get_AudioSystem();
    bool checkAudioAvailable(QString encoder);
    QStringList getAllDevices();
    void getDevices(QStringList &ls, QString soundtype, bool isName = false);
    bool isAvailable();
    void setEncoderAndAudioCodec();

private:
    QString m_screen;
    FORMAT m_format;
    QString m_resolution; //清晰度
    double m_bitrate = 8000;
    int m_fps = 25;
    QString m_microphoneName = "";  //选择的麦克风设备名字
    QString m_NoMic = tr("none mic");
//    int outputWidth = 0;
//    int outputHeight = 0;

    QString m_showPointer = "true";
    QString m_xid = "";
    QString m_encoder = "x264enc";
    bool m_isFullscreen = false;
    bool m_isWindow = false;
    bool m_openh264ProfileTest = false;
    int m_qpmin = 8;
    int m_qpmax = 8;
    QString m_speedpreset = "ultrafast";
    QString m_profile = "baseline";
    const QString gstr_Pipe = " ! ";
    QString m_sMuxer = "mp4mux";
    QString m_sFormat = "mp4";
    QString m_sAudioCodec = "lamemp3enc"; // lamemp3enc、opusenc
    QString m_sVideoPath;                       //保存路径
    GstElement *gstPipeline = nullptr;

    double m_scale = 1;
    int m_x=0, m_y=0, m_width=200, m_height=100;
    int m_right, m_bottom;
    QString m_completePathRec;

    bool m_closeMicrophone = false;             // 关闭麦克风
    QList<GstElement *> m_micSourceList;        // 麦克风源
    GstElement *pipelineMic = nullptr;             //音量的
    QHash<QString, QString> mapNameToDevice;
    pa_mainloop* m_mainloop = nullptr;
    pa_context* m_context = nullptr;

};

#endif // CORE_H
